class DrivingLesson < ActiveRecord::Base
  belongs_to :timeslot
  belongs_to :car
  belongs_to :topic

  validates :grade, :presence => true
  validates :grade, :inclusion => { :in => 1..5}
  validates :timeslot, :presence => true
  validates :topic, :presence => true
  validates :car, :presence => true

  validate :cannot_create_before_lesson_ended

  def cannot_create_before_lesson_ended
    if timeslot.end_time > Time.now
      errors.add(:base, "Can't create before lesson has ended")
    end
  end

  def instructor
    timeslot.instructor
  end

  def student
    timeslot.active_registration.student
  end

  def date
    timeslot.date
  end

  def start_time
    timeslot.start_time_string
  end

  def end_time
    timeslot.end_time_string
  end
end
